#ifndef QVIEWIMAGE_H
#define QVIEWIMAGE_H

#include <QWidget>
#include <QImage>
#include <QWheelEvent>


enum PAINTER_TOOL_TYPE{
    PAN,
    ADD_VIRTUAL_WALL,
    ADD_FORBIDDEN_ZONE
};

class QViewImage :public QWidget
{
    Q_OBJECT
public:
    QViewImage(QWidget*parent = 0);
    ~QViewImage(void);

    void setImage(const QString fullPath);
    void setPainterToolType(PAINTER_TOOL_TYPE type);

    QPointF screenToWorld(QPointF &pt, bool includeRotation = true);
    QPointF worldToScreen(QPointF &pt, bool includeRotation = true);

    void zoomToFullExtent();
    void saveData();

    void getVirtualWalls(QVector<QVector<QPointF> > &vec);
    void setVirtualWalls(const QJsonObject &obj);
private:
    QImage* mSourceImage;

    qreal mScale;
    qreal mRotation;
    QPointF mScaleBasePt;

    QPointF mLastPressedMousePt;

    bool mButtonLeftPressed = false;
    bool mButtonRightPressed = false;

    PAINTER_TOOL_TYPE mPainterType = PAINTER_TOOL_TYPE::PAN;

    bool mVirtualWallStart = false;
    QVector<QPointF> mVirtualWall;
    QVector<QPointF> mVirtualWallTmp;
    QVector<QVector<QPointF>> mVirtualWallVec;

    bool mForbiddenZoneStart = false;
    QVector<QPointF> mForbiddenZone;
    QVector<QVector<QPointF>> mForbiddenZoneVec;





    void updateVirtualWalls();
    void paintLinesInSrcImage(const QVector<QLineF> lines);
    void paintPointInSrcImage(const QPointF point);
    bool checkLineInImage(const QLineF &line);

    bool checkPointInImage(const QPointF &pt);
    void addVirtualWalls();
    void dealScaleAction(const QPointF &pt, const QPoint &angle);
protected:
    virtual void paintEvent(QPaintEvent * event);
    void wheelEvent(QWheelEvent *event);
    virtual void resizeEvent(QResizeEvent * event);
    virtual void mouseMoveEvent(QMouseEvent * event);
    virtual void mousePressEvent(QMouseEvent * event);
    virtual void mouseReleaseEvent(QMouseEvent * event);
    virtual void keyPressEvent(QKeyEvent * event);
    virtual void mouseDoubleClickEvent(QMouseEvent * event);

    QTransform getTransform(bool includeRotation = true);

signals:
    void emitSendData(QVector<QVector<QPointF>> vec, int w, int h);
    void emitSaveData();

};

#endif // QVIEWIMAGE_H
